/*
 * @Description: 盒管理上下文
 * @Author: Rfan
 * @Date: 2022-10-29 15:50:22
 * @LastEditTime: 2022-10-29 16:54:47
 */

import { createContext, useContext, useEffect, useReducer } from 'react';
import {
  SET_SELECT_AT,
  SET_SELECT_AT_ID,
  SET_SELECT_FOND,
  SET_SELECT_FOND_ID,
  SET_SELECT_NODE_ID,
  SET_BOX_LIST,
  ADD_BOX,
  EDIT_BOX
} from '../constants';
import { BoxMgtReducer } from '../reducers/BoxMgtReducer';
import { message, Modal } from 'antd';
import { addBox, deleteBox, editBox, fetchBoxListByPage } from '@/api/box/boxMgt';
import { DELETE_BATCH_ITEM, SET_SELECTED_ITEM } from '@/view/accession/BatchManage/constants';

const initialState = {
  // 当前选中的项目
  selectFond: {},
  selectAT: {},
  selectFondId: 0,
  selectATId: 0,
  selectNodeId: 0,

  // 选中的表格条目
  selectItems: [],
  // 盒列表
  list: []
};

const BoxMgtContext = createContext();

const BoxMgtContextProvider = (props) => {
  const [state, dispatch] = useReducer(BoxMgtReducer, initialState);

  const actions = {
    /**
     * @description: 设置表格中选中条目的key
     * @param {*} selectArr
     * @return {*}
     */
    setSelectKey(selectArr) {
      dispatch({ type: SET_SELECTED_ITEM, data: selectArr });
    },

    /**
     * @description: 设置选中的全宗
     */
    setSelectFond(data) {
      dispatch({ type: SET_SELECT_FOND, data });
    },

    /**
     * @description: 设置选中的档案类型
     */
    setSelectAT(data) {
      dispatch({ type: SET_SELECT_AT, data });
    },

    /**
     * @description: 设置选中的全宗
     */
    setSelectFondId(id) {
      dispatch({ type: SET_SELECT_FOND_ID, id });
    },

    /**
     * @description: 设置选中的档案类型ID
     */
    setSelectATId(id) {
      dispatch({ type: SET_SELECT_AT_ID, id });
    },

    /**
     * @description: 设置选中的树ID
     */
    setTreeNodeId(id) {
      dispatch({ type: SET_SELECT_NODE_ID, id });
    },

    getBoxList(params) {
      return new Promise((resolve, reject) => {
        fetchBoxListByPage(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: SET_BOX_LIST, data });
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },

    addBoxFunc(params) {
      return new Promise((resolve) => {
        addBox(params).then((res) => {
          const { data } = res;
          dispatch({ type: ADD_BOX, data });
          resolve(data);
        });
      });
    },

    editBoxFunc(params) {
      return new Promise((resolve) => {
        editBox(params).then((res) => {
          const { data } = res;
          dispatch({ type: EDIT_BOX, data });
          resolve(data);
        });
      });
    },

    delBoxFunc(id, title = '确认删除选中的盒？') {
      Modal.confirm({
        title: '删除',
        content: title,
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          deleteBox(ids).then((res) => {
            message.success('删除成功');
            const data = res.data;
            dispatch({ type: DELETE_BATCH_ITEM, ids: data });
          });
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    }
  };

  useEffect(() => {}, []);

  return (
    <BoxMgtContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </BoxMgtContext.Provider>
  );
};

export const useBoxMgtContext = () => {
  return useContext(BoxMgtContext);
};

export { BoxMgtContext, BoxMgtContextProvider };
